
package com.hex.ds.code.detection.metadata.service.impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hex.ds.code.detection.common.constants.CommonConstants;
import com.hex.ds.code.detection.common.service.ClusterSparingConfService;
import com.hex.ds.code.detection.metadata.dao.ScanMetadataMapper;
import com.hex.ds.code.detection.metadata.po.ScanMetadata;
import com.hex.ds.code.detection.metadata.service.ScanMetadataService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 元数据扫描表
 *
 * @author yemw
 * @date 2023-08-28 15:44:04
 */
@Service
@AllArgsConstructor
public class ScanMetadataServiceImpl extends ServiceImpl<ScanMetadataMapper, ScanMetadata> implements
    ScanMetadataService {


    private final ClusterSparingConfService clusterSparingConfService;

    private final ScanMetadataMapper scanMetadataMapper;


    @Override
    public List<ScanMetadata> wrapperScanMetadata(LambdaQueryWrapper<ScanMetadata> wrapper){
        Date today = DateUtil.date();
        Date yesterday = DateUtil.yesterday();
        Date before = DateUtil.offset(today, DateField.DAY_OF_MONTH, -2);
        //当天 前一天的触发时间到今天的触发时间
        List<ScanMetadata> scanMetadataList1 = this.baseMapper.selectList(wrapper.between(ScanMetadata::getCreateTime,yesterday,today).eq(ScanMetadata::getDel,CommonConstants.STATUS_NORMAL));
        //前一天 前两天的触发时间到前一天的触发时间
        List<ScanMetadata> scanMetadataList2 = this.baseMapper.selectList(wrapper.between(ScanMetadata::getCreateTime,before,yesterday).eq(ScanMetadata::getDel,CommonConstants.STATUS_NORMAL));
        //差集 当天对比前一天的hdfs路径
        List<ScanMetadata> dis = scanMetadataList1.stream()
                .filter(item -> !scanMetadataList2.stream()
                        .map( e -> e.getLocation())
                        .collect(Collectors.toList())
                        .contains(item.getLocation())).collect(Collectors.toList());
        return  dis;
    }

    /**
     * @Method queryTimePartitionTable
     * @Param
     * @Return java.util.List<java.lang.String>
     * @Description 获取以执行时间分区字段分区的表
     * @Author Wang zhihao
     * @Date 2023/11/7 20:17
     * @Version V1.0
     */
    @Override
    public List<String> queryTimePartitionTable() {
        return scanMetadataMapper.queryTimePartitionTable();
    }

}
